package com.sos.core.game.client
{
	import com.sos.utils.ClassUtils;

	public final class GameClientStatus
	{
		
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------
		
		public static const NOT_CONNECTED:GameClientStatus	= new GameClientStatus(0, "NOT_CONNECTED");
		public static const CONNECTING:GameClientStatus		= new GameClientStatus(1, "CONNECTING");
		public static const READY:GameClientStatus			= new GameClientStatus(2, "READY");
		public static const REQUEST:GameClientStatus 		= new GameClientStatus(3, "REQUEST");
		public static const POLLING:GameClientStatus 		= new GameClientStatus(4, "POLLING");
		public static const HEARTBEAT:GameClientStatus		= new GameClientStatus(5, "HEARTBEAT");
		
		// -------------------------------------------------------------------------------
		// STATIC
		// -------------------------------------------------------------------------------
		
		// Fields
		private static var _initialized:Boolean = false;
		private static var _list:Vector.<GameClientStatus>;
		
		// Fake static constructor
		{
			// Fill the internal list
			_list = new Vector.<GameClientStatus>();
			_list.push(GameClientStatus.READY);
			_list.push(GameClientStatus.REQUEST);
			_list.push(GameClientStatus.POLLING);
			_list.push(GameClientStatus.HEARTBEAT);
			_initialized = true;
		}
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		// Fields		
		private var _name:String;
		private var _id:uint;
		
		
		// -------------------------------------------------------------------------------
		// CONSTRUCTORS
		// -------------------------------------------------------------------------------

		/**
		 * Fake private constructor, accessed only by class loader 1 time 
		 * @param name Game client status name 
		 * @param value Game client value
		 */	
		public function GameClientStatus(id:uint, name:String)
		{
			// Safe unique instance lock
			if (_initialized) throw new Error("GameClientStatus enum already created!");
			// Init
			_name	= name;
			_id		= id;
		}
		
		// -------------------------------------------------------------------------------
		// PROPERTIES
		// -------------------------------------------------------------------------------              
		
		public function get id():uint
		{
			return _id;
		}
		
		public function get name():String
		{
			return _name;
		}
		
		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------
		
		public function toString():String
		{
			return ClassUtils.getName(this) + "." + _name;
		}
		
		// -------------------------------------------------------------------------------
		// STATIC METHODS
		// -------------------------------------------------------------------------------
		
		/**
		 * Returns the list of the names of 
		 * @return 
		 */
		public static function names():Array
		{
			var list:Array  = new Array;
			// Loop over values list
			for (var i:int=0; i<_list.length; i++)
			{
				list.push(_list[i].name);
			}
			return list;
		}
		
		/**
		 * 
		 * @return 
		 */
		public static function list():Array
		{
			var list:Array = new Array;
			// Loop over values list
			for (var i:int=0; i<_list.length; i++)
			{
				list.push(_list[i]);
			}
			return list;
		}
		
		/**
		 * Search a GameClientStatus by given id 
		 * @param id Id of the card type to find
		 * @return PlayerGameClientStatus constant for the given id, null if no GameClientStatus
		 * for the given id has been found
		 */             
		public static function getById(id:uint):GameClientStatus
		{
			// Search the value into the list
			for (var i:int=0; i<_list.length; i++)
			{
				if (_list[i].id == id) return _list[i];
			}
			throw new Error("GameClientStatus for id '" + id + "' unknown!");
		}
		
		/**
		 * Search a GameClientStatus by given name 
		 * @param name Name of the player GameClientStatus to find
		 * @return PlayerGameClientStatus constant for the given id, null if no GameClientStatus
		 * for the given name has been found
		 */             
		public static function getByName(name:String):GameClientStatus
		{
			// Search the value into the list
			for (var i:int=0; i<_list.length; i++)
			{
				if (_list[i].name == name) return _list[i];
			}
			throw new Error("GameClientStatus for name '" + name + "' unknown!");
		}
		
	}
}